<template>
	<view>
		<!-- 弹出层 -->
		<view :class="['scroll-popup', isShow ? 'scroll-open' : '', animation ? 'scroll-animation' : '']">
			<view class="scroll-box">
				<view class="scroll-top">
					<view class="scroll-top-left flex_ld">
						<view class="quxiao flex_c" @click="isShow = false">取消</view>
						<view class=" f32 c3 fw7">选择门店品类</view>
						<view class="queding flex_c" v-if="oneId && twoId && threeId" @click="getResult">确定</view>
						<view class="quxiao flex_c" v-else @click="noresult" >确定</view>
					</view>
					
				</view>

				<view class=" flex_dq">
					<scroll-view class="scroll-view_H" scroll-y="true">
						<view class="scroll-view-grid-box"
							v-if="checkBox && checkBox.length && checkBox[tabId] && checkBox[tabId].length">
							<view v-for="(item, index) in checkBox[tabId]" :key="index" @click="check(item)"
								:class="oneId == item.id ? 'scroll-view-item-true' : 'scroll-view-item'">
								{{ item.name || '' }}
							</view>
						</view>
						<view class="scroll-view-noBox" v-else>
							<image :src="baseUrl + '/static/image/noData1.png'" mode="aspectFill"></image>
							<view class="text">暂无数据</view>
						</view>
					</scroll-view>
					<scroll-view class="scroll-view_H" scroll-y="true">
						<view class="scroll-view-grid-box"
							v-if="checkBox && checkBox.length && checkBox[tabId] && checkBox[tabId].length">
							<view v-for="(item, index) in levelTwoList" :key="index" @click="check2(item)"
								:class="twoId == item.id ? 'scroll-view-item-true' : 'scroll-view-item'">
								{{ item.name || '' }}
							</view>
						</view>
						<view class="scroll-view-noBox" v-else>
							<image :src="baseUrl + '/static/image/noData1.png'" mode="aspectFill"></image>
							<view class="text">暂无数据</view>
						</view>
					</scroll-view>
					<scroll-view class="scroll-view_H" scroll-y="true">
						<view class="scroll-view-grid-box"
							v-if="checkBox && checkBox.length && checkBox[tabId] && checkBox[tabId].length">
							<view v-for="(item, index) in levelThreeList" :key="index" @click="check3(item)"
								:class="threeId == item.id ? 'scroll-view-item-true' : 'scroll-view-item'">
								{{ item.name || '' }}
							</view>
						</view>
						<view class="scroll-view-noBox" v-else>
							<image :src="baseUrl + '/static/image/noData1.png'" mode="aspectFill"></image>
							<view class="text">暂无数据</view>
						</view>
					</scroll-view>
				</view>
				<view v-show="safeArea" class="scroll-temp"></view>
			</view>
		</view>
		<!-- 遮罩层 -->
		<view v-show="isShow" class="scroll-mask" @click.stop="isMask ? close() : ''"></view>
	</view>
</template>

<script>
	import {categoryList} from "../api/other.js"
	export default {
		props: {
			//是否点击阴影关闭
			isMask: {
				type: Boolean,
				default: true,
			},
			//是否开启动画
			animation: {
				type: Boolean,
				default: true,
			},
			//是否开启安全条
			safeArea: {
				type: Boolean,
				default: true,
			},
		},
		data() {
			return {
				baseUrl: this.$imageBaseUrl,
				isShow: false,
				checkBox: [],
				noDataImg: '',
				nextImg: '',
				oneId:'',//一级
				onename:'',
				twoId:'',//二级
				twoname:'',
				threeId:'',//三级
				threename:'',
				temporary:[],
				tabId: 0, //计算当前顶部滑块id
				levelTwoList:[],
				levelThreeList:[],
				checkArr: [],
				id: 0, //通tabId，他们2的区别是，id先赋值，tabId在数据请求成功后才会赋值
				tabList: [{
						title: '请选择一级分类',
						id: 0
					},
					{
						title: '请选择二级分类',
						id: 1
					},
					{
						title: '请选择三级分类',
						id: 2
					}
				],
				scrollViewWidth: 0,
				elWidth: 0,
			};
		},
		
		methods: {
			open() {
				this.isShow = true;
				this.init();
			},
			close() {
				this.isShow = false;
			},
			init() {
				// 初始化数据
				this.id = 0;
				this.tabId = 0;
				this.twoId = ''
				this.threeId = ''
				this.levelTwoList = []
				this.levelThreeList = []
				this.temporary = []
				this.checkBox = [];
				this.checkArr = [];
				//初始化求出滚动的宽度
				// let view = uni.createSelectorQuery().in(this).select(".scroll-title")
				// view.boundingClientRect(rect => {
				// 	this.scrollViewWidth = Math.round(rect.width)
				// }).exec()
				this.getData();
			},
			async check(item) {
				console.log(item);
				this.oneId = item.id
				this.onename = item.name
				this.threeId = ''
				this.threename = ''
				this.levelThreeList = []
				this.erjiList()
				// this.$set(this.checkArr, this.id, this.checkBox[this.id][index]);
				// if (this.id < this.tabList.length - 1) this.id = this.id + 1;
				// await this.getData(); //同步请求
				// //获取点击元素的宽度求出元素一半的宽度
				// let view = uni.createSelectorQuery().in(this).select('#se-' + this.tabId)
				// view.boundingClientRect(rect => {
				// 	this.elWidth = Math.round(rect.width);
				// }).exec()
				// //定时器别删，不然最后一步有Bug
				// setTimeout(() => {
				// 	this.scrollViewWidth = this.scrollViewWidth + this.elWidth;
				// });
				// if (this.tabId < this.tabList.length - 1) this.tabId = this.tabId + 1;
			},
			check2(item){
				console.log(item);
				this.twoId = item.id
				this.threeId = ''
				this.threename = ''
				this.twoname = item.name
				this.sanjiList()
			},
			check3(item){
				console.log(item);
				this.threeId = item.id
				this.threename = item.name
			},
			checkTab(e) {
				console.log(e);
				if (e == this.id) return;
				this.id = e;
				this.tabId = e;
				this.checkArr = this.checkArr.splice(0, e);
			},

			//使用本地假数据进行加载
			async getData() {
				console.log(this.checkArr);
				console.log('长度',this.checkArr.length );
				// 接口请求数据实例--------------  三级联动就写3 依次类推
				if (this.checkArr.length < 3) {
					console.log('执行');
					let data = {
						category_id: this.checkArr && this.checkArr.length > 0 ? this.checkArr[this.checkArr.length - 1].id : ''
					};
					categoryList(data).then(res => {
						console.log(res);
						this.oneId = res.data[0].id
						this.onename = res.data[0].name
						this.erjiList()
						
						if (res.code == 1) {
							this.$set(this.checkBox, this.id, res.data);
						} else {
							this.$set(this.checkBox, this.id, []);
						}
					})
				} else {
					console.log('超出 停止掉接口 关闭弹窗 ');
					this.isShow = false;
					if (this.checkArr.length != this.tabList.length) return;
					let result = this.checkArr;
					this.$emit('change', {
						value: result
					})
				}

			},
			
			// 二级
			erjiList(){
				let data = {
					category_id:this.oneId
				}
				categoryList(data).then(res=>{
					console.log(res);
					if(res.code == 1){
						this.levelTwoList = res.data
					}
				})
			},
			
			// 三级
			sanjiList(){
				let data = {
					category_id:this.twoId
				}
				categoryList(data).then(res=>{
					console.log(res);
					if(res.code == 1){
						this.levelThreeList = res.data
					}
				})
			},
			noresult(){
				uni.$u.toast('请选择门店分类')
			},
			// 提交
			getResult() {
					this.temporary.push(this.onename,this.twoname,this.threename)
					let result = {
						classify_id : this.threeId,
						nameInfo:this.temporary
					};
					this.$emit('change', {
						value: result
					})
					this.close();
			},
		},
	};
</script>

<style>
	/deep/ ::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
		display: none;
	}
</style>
<style lang="scss" scoped>
	.scroll-popup {
		width: 100%;
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		bottom: -100%;
		z-index: 999;
	}

	.scroll-open {
		bottom: 0px !important;
	}

	.scroll-animation {
		transition: all 0.25s linear;
	}

	.scroll-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.3);
		z-index: 998;
	}

	.scroll-box {
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		bottom: 0;
		width: 100%;
		background: #ffff;
		border-radius: 24rpx 24rpx 0 0;
	}

	.scroll-temp {
		padding-bottom: env(safe-area-inset-bottom);
	}

	
	.scroll-top {
		// height: 56rpx;
		// line-height: 56rpx;
	}

	.scroll-top-left {
		width: 100%;
		padding: 30rpx 24rpx;
		border-bottom: 1rpx solid #f1f1f1;
	}
	.quxiao{
		padding: 4rpx 20rpx;
		font-size: 30rpx;
		// background: #f7f7f7;
		color: #999999;
		border-radius: 10rpx;
	}
	.queding{
		padding: 4rpx 20rpx;
		text-align: center;
		font-size: 30rpx;
		background-color: #ffda63;
		color: #000000;
		border-radius: 10rpx;
	}

	.scroll-top-right {
		float: right;
		padding-right: 24rpx;
		color: #A68A64;
		font-size: 28rpx;
	}

	.scroll-title {
		white-space: nowrap;
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		// background-color: #fafafa;
		border-bottom: 1rpx solid #e8e8e8;
	}

	.scroll-view_H {
		white-space: nowrap;
		// width: 100%;
		width: 33%;
		height: 900rpx;
		line-height: 100rpx;
		background-color: #ffffff;
	}

	.scroll-title-item {
		position: relative;

		image {
			position: absolute !important;
			right: -48rpx !important;
			top: 0 !important;
			bottom: 0 !important;
			width: 28rpx !important;
			height: 28rpx !important;
			margin: auto !important;
		}
	}

	.scroll-title-item-box {
		display: inline-block;
		margin: 0 32rpx;
		font-size: 28rpx;
		color: #333333;
	}

	.scroll-title-item-true {
		font-size: 28rpx;
		font-weight: 700;
		color: #A68A64;
	}

	.scroll-view-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		margin: 16rpx;
	}

	.scroll-view-grid-box {
		width: calc(100% - 10rpx);
		margin: 10rpx;
		padding-bottom: 10rpx;
	}

	.scroll-view-noBox {
		width: 100%;
		height: 400rpx;
		text-align: center;

		image {
			width: 400rpx;
			height: 260rpx;
			margin-top: 32rpx;
		}

		.text {
			width: 100%;
			text-align: center;
			color: #888;
			font-size: 28rpx;
			margin-top: -40rpx;
		}
	}

	.scroll-view-item {
		padding: 0rpx 24rpx;
		text-align: left;
		border-radius: 6rpx;
		background: #f7f7f7;
		color: #333333;
		font-size: 28rpx;
		margin: 12rpx 4rpx;
		height: 66rpx;
		line-height: 66rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.scroll-view-item-true {
		padding: 0rpx 24rpx;
		text-align: left;
		border-radius: 6rpx;
		background-color: #ffda63;
		color: #000000;
		font-size: 28rpx;
		margin: 12rpx 4rpx;
		height: 66rpx;
		line-height: 66rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
</style>